package com.chexiehui.jc.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.utils.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;

public abstract class PengYouQuanFlagView extends ViewGroup {
    protected abstract void displayImage(int position, ImageView imageView, String url);

    protected abstract void onClickImage(int position, String url, ImageView imageView, List<String> imgList);

    private int customInterval = 15;//默认的间距
    private int muiHeight, muiWidth = 0, sinHeight = 0, sinWidth = 0;//多图的时候高度，宽度，单图时候宽高
    private List<String> imgList = new ArrayList<>();//图片的集合
    private Context mContext = null;
    private List<ImageView> imageViewList;

    public PengYouQuanFlagView(Context context) {
        this(context, null);
    }

    public PengYouQuanFlagView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PengYouQuanFlagView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        //初始化自定义属性
        initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attributeSet) {
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.PengYouQuanFlagView);
        customInterval = (int) typedArray.getDimension(R.styleable.PengYouQuanFlagView_pengInterval, SizeUtils.dp2px(10));//默认的边距
        muiWidth = (int) typedArray.getDimension(R.styleable.PengYouQuanFlagView_muiPengWidth, getResources().getDimensionPixelSize(R.dimen.dp_93));//多图的时候高度
        muiHeight = (int) typedArray.getDimension(R.styleable.PengYouQuanFlagView_muiPengHeight, getResources().getDimensionPixelSize(R.dimen.dp_93));//多图的时候高度
        sinWidth = (int) typedArray.getDimension(R.styleable.PengYouQuanFlagView_sinPengWidth, getResources().getDimensionPixelSize(R.dimen.dp_230));//单图的时候宽度
        sinHeight = (int) typedArray.getDimension(R.styleable.PengYouQuanFlagView_sinPengHeight, getResources().getDimensionPixelSize(R.dimen.dp_193));//单图的时候高度
        imageViewList = new ArrayList<>();

        for (int i = 0; i < 9; i++) {
            imageViewList.add(creatImageView(i));
        }

        typedArray.recycle();
    }

    public void setMuiHeight(int muiHeight) {
        this.muiHeight = muiHeight;
    }

    public void setMuiWidth(int muiWidth) {
        this.muiWidth = muiWidth;
    }

    /**
     * @date: 2019/5/21 0021
     * @author: gaoxiaoxiong
     * @description:设置图片地址链接
     **/
    public void setImgList(List<String> list) {
        if (list == null || list.size() <= 0) return;
        imgList.clear();
        imgList.addAll(list);
        resetLayout();
    }

    /**
     * @date: 2019/5/21 0021
     * @author: gaoxiaoxiong
     * @description: 重新添加布局
     **/
    protected void resetLayout() {
        removeAllViews();
        for (int i = 0; i < imgList.size(); i++) {
            ImageView imageView = imageViewList.get(i);
            addView(imageView);
            displayImage(i, imageView, imgList.get(i));
        }
    }

    /**
     * @date: 2019/5/21 0021
     * @author: gaoxiaoxiong
     * @description:创建图片
     **/
    public ImageView creatImageView(int position) {
        int childCount = imgList.size();
        CustomRoundAngleImageView imageView = new CustomRoundAngleImageView(mContext);
        imageView.setRadius(SizeUtils.dp2px(4));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        if (childCount == 1) {
            params.width = sinWidth;
            params.height = sinHeight;
        } else {
            params.width = muiWidth;
            params.height = muiHeight;
        }
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                onClickImage(position, imgList.get(position), (ImageView) view, imgList);
            }
        });
        return imageView;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mParentMearWidht = MeasureSpec.getSize(widthMeasureSpec);
        int line = 1;
        int currentWidth = 0, childHeight = 0;//子view的宽度;,子view的高度
        int childCount = getChildCount();
        if (childCount <= 0) {
            return;
        }
        for (int i = 0; i < childCount; i++) {
            View imageView = getChildAt(i);
            measureChild(imageView, widthMeasureSpec, heightMeasureSpec);
            int imageWidth = 0;
            //中间的图片
            if (i == 1 || i == 4 || i == 7) {
                imageWidth = imageView.getMeasuredWidth() + customInterval * 2;
            } else {//不是中间的图片
                imageWidth = imageView.getMeasuredWidth();
            }
            childHeight = imageView.getMeasuredHeight() + customInterval * 2;
            if (mParentMearWidht - currentWidth >= imageWidth) {
                currentWidth = currentWidth + imageWidth;
            } else {//需要换行
                line = line + 1;
                currentWidth = 0;
            }
        }
        //再次校验高度是否正确
        View imageView = getChildAt(0);
        int liYingHeight = 0;
        if (childCount >= 0 && childCount <= 3) {
            liYingHeight = (imageView.getMeasuredHeight() + customInterval * 2) * 1;
        } else if (childCount >= 4 && childCount <= 6) {
            //理应高度
            liYingHeight = (imageView.getMeasuredHeight() + customInterval * 2) * 2;
        } else if (childCount >= 7) {
            //理应高度
            liYingHeight = (imageView.getMeasuredHeight() + customInterval * 2) * 3;
        }
        //代码计算高度
        int jisuanHeight = childHeight * line;
        if (liYingHeight != jisuanHeight) {
            if (childCount >= 0 && childCount <= 3) {
                line = 1;
            } else if (childCount >= 4 && childCount <= 6) {
                line = 2;
            } else if (childCount >= 7) {
                line = 3;
            }
        }

        setMeasuredDimension(mParentMearWidht, childHeight * line);
    }


    @Override
    protected void onLayout(boolean b, int i0, int i1, int i2, int i3) {
        int childCount = getChildCount();
        //整个View的宽度
        int mParentWidth = this.getMeasuredWidth();
        int left = 0, top = 0;
        for (int i = 0; i < childCount; i++) {
            View imageView = getChildAt(i);
            //获取子View的宽高，判断一行是否可以放的下，如果放不下，需要换行
            int childHeight = imageView.getMeasuredHeight();
            int childWidth = imageView.getMeasuredWidth();
            if (mParentWidth - left >= childWidth) {
                imageView.layout(left, top, left + childWidth, top + childHeight);
                left = left + childWidth + customInterval;
            } else {
                left = 0;
                top = top + childHeight + customInterval;
                imageView.layout(left, top, left + childWidth, top + childHeight);
                left = childWidth + customInterval;
            }
        }
    }
}
